﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using Scream.AI;

namespace AI.WebService {
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService {

        private Scream.AI.AI guru = new Scream.AI.AI();

        [WebMethod]
        public void StoreObject(string key, object obj) {
            guru.DataStorage.StoreObject(key, obj);
        }

        [WebMethod]
        public void RemoveObject(string key) {
            guru.DataStorage.Objects.Remove(key);
        }

        [WebMethod]
        public DecisionEntity GetDecision() {
            guru.MakeDecision();
            guru.DecideActions();
            return guru.Decision;
        }

        [WebMethod]
        public void AddRule(RuleEntity rule) {
            guru.RulesStorage.Rules.Add(rule);
        }

        [WebMethod]
        public void RemoveRule(Guid ruleId) {
            RuleEntity rule = guru.RulesStorage.Rules.Where(r => r.Id == ruleId).SingleOrDefault();
            if (rule != null) {
                guru.RulesStorage.Rules.Remove(rule);
            }
        }

        [WebMethod]
        public void AddScheduleRule(ScheduleRuleEntity scheduleRule) {
            guru.ScheduleRuleStorage.ScheduleRules.Add(scheduleRule);
        }

        [WebMethod]
        public void RemoveScheduleRule(Guid scheduleRuleId) {
            ScheduleRuleEntity scheduleRule = guru.ScheduleRuleStorage.ScheduleRules.Where(r => r.RuleImportance.ImportanceRule.Rule.Id == scheduleRuleId).SingleOrDefault();
            if (scheduleRule != null) {
                guru.ScheduleRuleStorage.ScheduleRules.Remove(scheduleRule);
            }
        }

        [WebMethod]
        public void AddConflict(ConflictEntity conflict) {
            guru.ConflictsStorage.Conflicts.Add(conflict);
        }

        [WebMethod]
        public void RemoveConflict(Guid conflictId) {
            ConflictEntity conflict = guru.ConflictsStorage.Conflicts.Where(c => c.Id == conflictId).SingleOrDefault();
            if (conflict != null) {
                guru.ConflictsStorage.Conflicts.Remove(conflict);
            }
        }

    }
}
